package com.polaris.live.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.text.Editable
import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.DialogGameCustomizeBinding
import com.polaris.live.im.manager.ImNoticeManager
import com.polaris.live.interface_bag.TextWatcherAdapter
import com.polaris.live.net.liveService
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.go_resp.GameCoinBean
import com.polaris.live.translation.R.*

/**
 * GameCustomizeDialog
 *
 * @author Created by 半仙 on 2024/3/13/013 15:51
 */
class GameCustomizeDialog(
    context: Context,
    private val mViewModel: BaseViewModel,
) : CenterPopupView(context) {
    override fun getImplLayoutId() = R.layout.dialog_game_customize

    private lateinit var mBinding: DialogGameCustomizeBinding

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        mBinding = DialogGameCustomizeBinding.bind(popupImplView)
        mBinding.userCoin.text =
            "${StringUtils.getString(string.diamond_balance)}: ${UserManager.getUser().account?.userCoins ?: 0}" + " "
        initListener()
    }

    private fun initListener() {
        ImNoticeManager.observeAccountChange(this) {
            mBinding.userCoin.text =
                "${StringUtils.getString(string.diamond_balance)}: ${UserManager.getUser().account?.userCoins ?: 0}" + " "
        }
        mBinding.editDiamond.addTextChangedListener(object : TextWatcherAdapter() {
            override fun afterTextChanged(s: Editable?) {
                var toString = mBinding.editDiamond.text.toString()

                if (toString.isEmpty()) {
                    mBinding.group.visibility = View.VISIBLE
                    mBinding.tvGameCoin.text = "0"
                } else {
                    val coin = toString.toLongOrNull() ?: return

                    if (coin > 1000000) {
                        mBinding.editDiamond.setText("1000000")
                        toString = "1000000"
                        mBinding.editDiamond.setSelection(mBinding.editDiamond.text.toString().length)
                    }
                    mBinding.group.visibility = View.GONE
                    mBinding.tvGameCoin.text = toString

                }
            }
        })

        mBinding.buyGameBeans.setOnSingleClickListener {
            val gameCoin = mBinding.editDiamond.text.toString()
            val coinLong = gameCoin.toLongOrNull() ?: return@setOnSingleClickListener
            if (coinLong > 0) {
                buyGameCoin(coinLong)
            }
        }
    }

    private fun buyGameCoin(coin: Long) {
        mViewModel.requestLoading(
            { liveService.gameCoinExchange(GameCoinBean(coin)) }, {
                ToastGravity.showGravity(StringUtils.getString(string.game_coin_success))
                dismiss()
            }, {
                if (it.errCode == CommonConst.ErrorCode.SENDGIFT) {
                    GlobalPopupManager.showRechargePopup(context, mViewModel, isNeedBottom = true)
                }
                ToastGravity.showGravity(it.errorMsg)
            }, waitTime = 0
        )
    }
}